FUNCTION YBC_SWS_SEND_FIRST .
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IV_APPID) TYPE  YBC_SWS_APPID
*"     VALUE(IV_TEMPLID) TYPE  YBC_SWS_TEMPLID
*"     VALUE(IV_BUSINESSID) TYPE  YBC_SWS_REQUEST-BUSINESSID
*"     VALUE(IV_UNAME) TYPE  SY-UNAME DEFAULT SY-UNAME
*"  EXPORTING
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"  TABLES
*"      IT_HEAD_DATA STRUCTURE  YBCS_SWS_DATA_H
*"      IT_ITEM_DATA STRUCTURE  YBCS_SWS_DATA_I OPTIONAL
*"      IT_APPROVER STRUCTURE  YBCS_SWS_APPROVER OPTIONAL
*"      IT_ATTACH TYPE  YBCT_SWS_ATTACH01 OPTIONAL
*"----------------------------------------------------------------------
  DATA:
        LV_PERSNUMBER TYPE AD_PERSNUM. "Person Number
  DATA:
    LS_APP         TYPE YBC_SWS_APP,
    LS_APP_CNFG    TYPE YBC_SWS_APP_CNFG,
    LT_APP_ACTION  TYPE STANDARD TABLE OF YBC_SWS_APP_ACT,
    LS_TEMPL       TYPE YBC_SWS_TEMPL,
    LT_TEMPL       TYPE STANDARD TABLE OF YBC_SWS_TEMPL,
    LS_TEMPL_H     TYPE YBC_SWS_TEMPL_H,
    LS_TEMPL_H_TMP TYPE YBC_SWS_TEMPL_H,
    LT_TEMPL_H     TYPE STANDARD TABLE OF YBC_SWS_TEMPL_H, "ALL
    LT_TEMPL_H1    TYPE STANDARD TABLE OF YBC_SWS_TEMPL_H, "ZH
    LT_TEMPL_H2    TYPE STANDARD TABLE OF YBC_SWS_TEMPL_H, "EN
    LT_TEMPL_H3    TYPE STANDARD TABLE OF YBC_SWS_TEMPL_H, "Other
    LS_TEMPL_I     TYPE YBC_SWS_TEMPL_I,
    LS_TEMPL_I_TMP TYPE YBC_SWS_TEMPL_I,
    LT_TEMPL_I     TYPE STANDARD TABLE OF YBC_SWS_TEMPL_I,
    LT_TEMPL_I1    TYPE STANDARD TABLE OF YBC_SWS_TEMPL_I,
    LT_TEMPL_I2    TYPE STANDARD TABLE OF YBC_SWS_TEMPL_I,
    LT_TEMPL_I3    TYPE STANDARD TABLE OF YBC_SWS_TEMPL_I,

    LT_ITEM_DATA   TYPE STANDARD TABLE OF YBCS_SWS_DATA_I,
    LT_SPLIT_FIELD TYPE STANDARD TABLE OF CHAR50,
    LS_FVALUE_H    TYPE YBCS_SWS_FVALUE,
    LS_FVALUE_H1   TYPE YBCS_SWS_FVALUE,
    LT_FVALUE_H    TYPE STANDARD TABLE OF YBCS_SWS_FVALUE,
    LT_FVALUE_H1   TYPE STANDARD TABLE OF YBCS_SWS_FVALUE, "未格式化的数据
    LS_ITEM_H      TYPE YBCS_SWS_ITEM_H,
    LT_ITEM_H      TYPE STANDARD TABLE OF YBCS_SWS_ITEM_H,
    LS_ITEM_I      TYPE YBCS_SWS_ITEM_I,
    LT_ITEM_I      TYPE STANDARD TABLE OF YBCS_SWS_ITEM_I.

  DATA:
    LS_USER      TYPE YBC_SWS_USER,
    LS_CREATOR   TYPE YBC_SWS_USER,
    LS_REQUEST   TYPE YBC_SWS_REQUEST,
    LS_REQHEAD   TYPE YBC_SWS_REQHEAD,
    LS_REQJSON   TYPE YBC_SWS_REQJSON,
    LS_ATTACH_DB TYPE YBC_SWS_ATTACH,
    LT_ATTACH_DB TYPE STANDARD TABLE OF YBC_SWS_ATTACH,
    LT_USER      TYPE STANDARD TABLE OF YBC_SWS_USER,
    LT_REQHEAD   TYPE STANDARD TABLE OF YBC_SWS_REQHEAD,
    LS_APPROVER  TYPE YBC_SWS_APPROVER,
    LT_APPROVER  TYPE STANDARD TABLE OF YBC_SWS_APPROVER.

  FIELD-SYMBOLS:
    <FS_VALUE> TYPE ANY,
    <FS_LINE>  TYPE ANY,
    <FT_TABLE> TYPE STANDARD TABLE.

  DATA:
    LV_VALUE        TYPE STRING,
    LV_VALUE_TMP    TYPE STRING,
    LR_STRUCT_TYP   TYPE REF TO  CL_ABAP_DATADESCR,
    LR_DYNTABLE_TYP TYPE REF TO  CL_ABAP_TABLEDESCR,
    LS_COMPONENT    TYPE CL_ABAP_STRUCTDESCR=>COMPONENT,
    LT_COMPONENT    TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE.

** Dynamic Table Declarations
  DATA : LT_DYN_TABLE TYPE REF TO DATA,
         LS_DYN_LINE  TYPE REF TO DATA.

  SELECT SINGLE
      *
      INTO LS_APP
      FROM YBC_SWS_APP
     WHERE APPID = IV_APPID.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    MESSAGE S003(YBC_SWS) INTO ES_MSG-MESSAGE WITH IV_APPID.
    RETURN.
  ENDIF.

  SELECT SINGLE
    *
    INTO LS_APP_CNFG
    FROM YBC_SWS_APP_CNFG
   WHERE APPID = IV_APPID.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    MESSAGE S004(YBC_SWS) INTO ES_MSG-MESSAGE WITH IV_APPID.
    RETURN.
  ENDIF.

  IF LS_APP_CNFG-WFCONFID IS INITIAL AND IT_APPROVER[] IS INITIAL..
    ES_MSG-TYPE = 'E'.
    MESSAGE S011(YBC_SWS) INTO ES_MSG-MESSAGE.
    RETURN.
  ENDIF.

  SELECT
     *
    INTO TABLE LT_APP_ACTION
    FROM YBC_SWS_APP_ACT
   WHERE APPID = IV_APPID.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    MESSAGE S015(YBC_SWS) INTO ES_MSG-MESSAGE WITH IV_APPID.
    RETURN.
  ENDIF.

  "取模板数据
  SELECT
    *
    INTO TABLE LT_TEMPL
    FROM YBC_SWS_TEMPL
   WHERE TEMPLATE = IV_TEMPLID.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    MESSAGE S005(YBC_SWS) INTO ES_MSG-MESSAGE WITH IV_APPID.
    RETURN.
  ENDIF.

  SELECT *
    INTO TABLE LT_TEMPL_H
    FROM YBC_SWS_TEMPL_H
     FOR ALL ENTRIES IN LT_TEMPL
   WHERE TEMPLATE = LT_TEMPL-TEMPLATE
     AND LANGUAGE = LT_TEMPL-LANGUAGE.

  SELECT *
    INTO TABLE LT_TEMPL_I
    FROM YBC_SWS_TEMPL_I
     FOR ALL ENTRIES IN LT_TEMPL
   WHERE TEMPLATE = LT_TEMPL-TEMPLATE
     AND LANGUAGE = LT_TEMPL-LANGUAGE.

  SORT LT_TEMPL_H BY TEMPLATE LANGUAGE HEADERNO.
  SORT LT_TEMPL_I BY TEMPLATE LANGUAGE HEADERNO ITEMNO.

  LOOP AT LT_TEMPL_H INTO LS_TEMPL_H.
    IF LS_TEMPL_H-LANGUAGE = '1'.
      APPEND LS_TEMPL_H TO LT_TEMPL_H1.
    ELSEIF LS_TEMPL_H-LANGUAGE = 'E'.
      APPEND LS_TEMPL_H TO LT_TEMPL_H2.
    ELSEIF LS_TEMPL_H-LANGUAGE = 'Z'.
      APPEND LS_TEMPL_H TO LT_TEMPL_H3.
    ELSE.
      APPEND LS_TEMPL_H TO LT_TEMPL_H1.
    ENDIF.
  ENDLOOP.

  LOOP AT LT_TEMPL_I INTO LS_TEMPL_I.
    IF LS_TEMPL_I-LANGUAGE = '1'.
      APPEND LS_TEMPL_I TO LT_TEMPL_I1.
    ELSEIF LS_TEMPL_I-LANGUAGE = 'E'.
      APPEND LS_TEMPL_I TO LT_TEMPL_I2.
    ELSEIF LS_TEMPL_I-LANGUAGE = 'Z'.
      APPEND LS_TEMPL_I TO LT_TEMPL_I3.
    ELSE.
      APPEND LS_TEMPL_I TO LT_TEMPL_I1.
    ENDIF.
  ENDLOOP.

  REFRESH LT_TEMPL_H[].
  IF LINES( LT_TEMPL_H1 ) < LINES( LT_TEMPL_H2 ).
    LT_TEMPL_H = LT_TEMPL_H2.
  ELSE.
    LT_TEMPL_H = LT_TEMPL_H1.
  ENDIF.

  IF LINES( LT_TEMPL_H ) < LINES( LT_TEMPL_H3 ).
    LT_TEMPL_H = LT_TEMPL_H3.
  ENDIF.

  REFRESH LT_TEMPL_I[].
  IF LINES( LT_TEMPL_I1 ) < LINES( LT_TEMPL_I2 ).
    LT_TEMPL_I = LT_TEMPL_I2.
  ELSE.
    LT_TEMPL_I = LT_TEMPL_I1.
  ENDIF.

  IF LINES( LT_TEMPL_I ) < LINES( LT_TEMPL_I3 ).
    LT_TEMPL_I = LT_TEMPL_I3.
  ENDIF.

  "准备抬头数据
  DELETE LT_TEMPL_H WHERE FIELD_NAME = 'ITEMS'. "删除为ITEM预留的字段
  SORT LT_TEMPL_H BY HEADERNO.
  LOOP AT LT_TEMPL_H INTO LS_TEMPL_H.
    CONDENSE LS_TEMPL_H-FIELD_NAME.
    CLEAR LS_FVALUE_H1.
    IF LS_TEMPL_H-FIELD_NAME CS '&'.

      CLEAR LS_FVALUE_H.
      LS_FVALUE_H-FIELD = LS_TEMPL_H-FIELD_NAME.
      " LS_FVALUE_H-PLATFORM = LS_TEMPL_H-PLATFORM.
      REFRESH LT_SPLIT_FIELD[].
      SPLIT LS_TEMPL_H-FIELD_NAME AT '&' INTO TABLE LT_SPLIT_FIELD.

      CLEAR LV_VALUE.
      LOOP AT LT_SPLIT_FIELD INTO DATA(LS_SPLIT_FIELD).

        CONDENSE LS_SPLIT_FIELD.
        CLEAR IT_HEAD_DATA.
        READ TABLE IT_HEAD_DATA WITH KEY FIELD = LS_SPLIT_FIELD.
        IF SY-SUBRC = 0.
          PERFORM FRM_FORMAT_VALUE
            USING LS_SPLIT_FIELD LS_TEMPL_H-ZFORMAT IT_HEAD_DATA-VALUE
            CHANGING LV_VALUE.
        ENDIF.

        CONDENSE LV_VALUE.
        CONCATENATE LS_FVALUE_H-VALUE LV_VALUE INTO LS_FVALUE_H-VALUE SEPARATED BY SPACE.

        "为审批条件做准备的数据
        CONDENSE IT_HEAD_DATA-VALUE.
        CLEAR LS_FVALUE_H1.
        LS_FVALUE_H1-FIELD = LS_SPLIT_FIELD.
        LS_FVALUE_H1-VALUE = IT_HEAD_DATA-VALUE.
        APPEND LS_FVALUE_H1 TO LT_FVALUE_H1.
      ENDLOOP.

      CONDENSE LS_FVALUE_H-VALUE.

    ELSE.
      CLEAR LS_FVALUE_H.
      LS_FVALUE_H-FIELD = LS_TEMPL_H-FIELD_NAME.
      " LS_FVALUE_H-PLATFORM = LS_TEMPL_H-PLATFORM.

      CLEAR IT_HEAD_DATA.
      READ TABLE IT_HEAD_DATA WITH KEY FIELD = LS_TEMPL_H-FIELD_NAME.
      IF SY-SUBRC = 0.
        PERFORM FRM_FORMAT_VALUE
          USING LS_TEMPL_H-FIELD_NAME LS_TEMPL_H-ZFORMAT IT_HEAD_DATA-VALUE
          CHANGING LS_FVALUE_H-VALUE.
      ENDIF.

    ENDIF.

    CLEAR LS_TEMPL_H_TMP.
    READ TABLE LT_TEMPL_H1 INTO LS_TEMPL_H_TMP WITH KEY FIELD_NAME = LS_TEMPL_H-FIELD_NAME.
    IF SY-SUBRC = 0.
      LS_FVALUE_H-NAME1 = LS_TEMPL_H_TMP-FIELD_DESC.
    ENDIF.

    CLEAR LS_TEMPL_H_TMP.
    READ TABLE LT_TEMPL_H2 INTO LS_TEMPL_H_TMP WITH KEY FIELD_NAME = LS_TEMPL_H-FIELD_NAME.
    IF SY-SUBRC = 0.
      LS_FVALUE_H-NAME2 = LS_TEMPL_H_TMP-FIELD_DESC.
    ENDIF.

    CLEAR LS_TEMPL_H_TMP.
    READ TABLE LT_TEMPL_H3 INTO LS_TEMPL_H_TMP WITH KEY FIELD_NAME = LS_TEMPL_H-FIELD_NAME.
    IF SY-SUBRC = 0.
      LS_FVALUE_H-NAME3 = LS_TEMPL_H_TMP-FIELD_DESC.
    ENDIF.

    APPEND LS_FVALUE_H TO LT_FVALUE_H.

    "为审批条件做准备的数据
    IF LS_FVALUE_H1 IS INITIAL.
      CONDENSE IT_HEAD_DATA-VALUE.
      LS_FVALUE_H1 = LS_FVALUE_H.
      LS_FVALUE_H1-VALUE = IT_HEAD_DATA-VALUE.
      APPEND LS_FVALUE_H1 TO LT_FVALUE_H1.
    ENDIF.
  ENDLOOP.

  "行项目数据编辑
  IF IT_ITEM_DATA[] IS NOT INITIAL.

*    "字段为空不显示 for zeekr IDP
*    LOOP AT IT_ITEM_DATA WHERE FIELD = 'AUFNR' AND VALUE IS INITIAL.
*      EXIT.
*    ENDLOOP.
*    IF SY-SUBRC = 0.
*      DELETE LT_TEMPL_I WHERE TEMPLATE = 'ZSWS_APP01' AND HEADERNO = '99' AND FIELD_NAME = 'AUFNR'.
*      DELETE LT_TEMPL_I WHERE TEMPLATE = 'ZSWS_APP01' AND HEADERNO = '99' AND FIELD_NAME = 'KTEXT2'.
*    ENDIF.
*
*    LOOP AT IT_ITEM_DATA WHERE FIELD = 'KOSTL' AND VALUE IS INITIAL.
*      EXIT.
*    ENDLOOP.
*    IF SY-SUBRC = 0.
*      DELETE LT_TEMPL_I WHERE TEMPLATE = 'ZSWS_APP01' AND HEADERNO = '99' AND FIELD_NAME = 'KOSTL'.
*      DELETE LT_TEMPL_I WHERE TEMPLATE = 'ZSWS_APP01' AND HEADERNO = '99' AND FIELD_NAME = 'KTEXT'.
*    ENDIF.
*
*    LOOP AT IT_ITEM_DATA WHERE FIELD = 'ANLN1' AND VALUE IS INITIAL.
*      EXIT.
*    ENDLOOP.
*    IF SY-SUBRC = 0.
*      DELETE LT_TEMPL_I WHERE TEMPLATE = 'ZSWS_APP01' AND HEADERNO = '99' AND FIELD_NAME = 'ANLN1'.
*      DELETE LT_TEMPL_I WHERE TEMPLATE = 'ZSWS_APP01' AND HEADERNO = '99' AND FIELD_NAME = 'TXT50'.
*    ENDIF.
    "End

    REFRESH LT_COMPONENT.
    SORT LT_TEMPL_I BY HEADERNO ITEMNO.
    LOOP AT LT_TEMPL_I INTO LS_TEMPL_I.
      "添加行项目标题信息
      CLEAR LS_ITEM_H.
      LS_ITEM_H-FIELD = LS_TEMPL_I-FIELD_NAME.
      LS_ITEM_H-PLATFORM = LS_TEMPL_I-PLATFORM.

      CLEAR LS_TEMPL_I_TMP.
      READ TABLE LT_TEMPL_I1 INTO LS_TEMPL_I_TMP WITH KEY FIELD_NAME = LS_TEMPL_I-FIELD_NAME.
      IF SY-SUBRC = 0.
        LS_ITEM_H-NAME1 = LS_TEMPL_I_TMP-FIELD_DESC.
      ENDIF.

      CLEAR LS_TEMPL_I_TMP.
      READ TABLE LT_TEMPL_I2 INTO LS_TEMPL_I_TMP WITH KEY FIELD_NAME = LS_TEMPL_I-FIELD_NAME.
      IF SY-SUBRC = 0.
        LS_ITEM_H-NAME2 = LS_TEMPL_I_TMP-FIELD_DESC.
      ENDIF.

      CLEAR LS_TEMPL_I_TMP.
      READ TABLE LT_TEMPL_I3 INTO LS_TEMPL_I_TMP WITH KEY FIELD_NAME = LS_TEMPL_I-FIELD_NAME.
      IF SY-SUBRC = 0.
        LS_ITEM_H-NAME3 = LS_TEMPL_I_TMP-FIELD_DESC.
      ENDIF.

      APPEND LS_ITEM_H TO LT_ITEM_H.

      "添加行项目表行字段信息
      CLEAR LS_COMPONENT.
      "字段中不能含有&,否则会Dump
      IF LS_TEMPL_I-FIELD_NAME CS '&'.
        REPLACE ALL OCCURRENCES OF '&' IN LS_TEMPL_I-FIELD_NAME WITH '_'.
      ENDIF.
      LS_COMPONENT-NAME = LS_TEMPL_I-FIELD_NAME.
      LS_COMPONENT-TYPE ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_NAME( 'TEXT1000' ).
      APPEND LS_COMPONENT TO LT_COMPONENT.
    ENDLOOP.

    IF LT_COMPONENT[] IS NOT INITIAL.

      LR_STRUCT_TYP  ?= CL_ABAP_STRUCTDESCR=>CREATE( P_COMPONENTS = LT_COMPONENT ).
      LR_DYNTABLE_TYP = CL_ABAP_TABLEDESCR=>CREATE( P_LINE_TYPE = LR_STRUCT_TYP ).

      CREATE DATA:
        LT_DYN_TABLE TYPE HANDLE LR_DYNTABLE_TYP,
        LS_DYN_LINE TYPE HANDLE LR_STRUCT_TYP.

      "生成动态内表
      ASSIGN LT_DYN_TABLE->* TO <FT_TABLE>.
      ASSIGN LS_DYN_LINE->* TO <FS_LINE>.

      SORT IT_ITEM_DATA BY ITEM.
      LT_ITEM_DATA = IT_ITEM_DATA[].

      LOOP AT IT_ITEM_DATA.
        "非拼接字段处理
        ASSIGN COMPONENT IT_ITEM_DATA-FIELD OF STRUCTURE <FS_LINE> TO <FS_VALUE>.
        IF SY-SUBRC = 0.
          CLEAR LS_TEMPL_I.
          READ TABLE LT_TEMPL_I INTO LS_TEMPL_I WITH KEY FIELD_NAME = IT_ITEM_DATA-FIELD.
          IF SY-SUBRC <> 0.
            <FS_VALUE> = IT_ITEM_DATA-VALUE.
          ELSE.
            PERFORM FRM_FORMAT_VALUE
                    USING LS_TEMPL_I-FIELD_NAME LS_TEMPL_I-ZFORMAT IT_ITEM_DATA-VALUE
                    CHANGING <FS_VALUE>.
          ENDIF.
        ENDIF.

        "拼接字段处理
        LOOP AT LT_TEMPL_I INTO LS_TEMPL_I WHERE FIELD_NAME CS '&' AND FIELD_NAME CS IT_ITEM_DATA-FIELD.
          CONDENSE LS_TEMPL_I-FIELD_NAME.

          REFRESH LT_SPLIT_FIELD[].
          SPLIT LS_TEMPL_I-FIELD_NAME AT '&' INTO TABLE LT_SPLIT_FIELD.

          CLEAR LV_VALUE.
          LOOP AT LT_SPLIT_FIELD INTO LS_SPLIT_FIELD.

            READ TABLE LT_ITEM_DATA INTO DATA(LS_ITEM_DATA) WITH KEY
              ITEM = IT_ITEM_DATA-ITEM FIELD =  LS_SPLIT_FIELD.
            IF SY-SUBRC = 0.
              "LV_VALUE_TMP = LS_ITEM_DATA-VALUE.
              PERFORM FRM_FORMAT_VALUE
                  USING LS_SPLIT_FIELD LS_TEMPL_I-ZFORMAT LS_ITEM_DATA-VALUE
                  CHANGING LV_VALUE_TMP.
            ENDIF.

            CONDENSE LV_VALUE_TMP.
            CONCATENATE LV_VALUE LV_VALUE_TMP INTO LV_VALUE SEPARATED BY SPACE.
          ENDLOOP.
          CONDENSE LV_VALUE.

          REPLACE ALL OCCURRENCES OF '&' IN LS_TEMPL_I-FIELD_NAME WITH '_'.
          ASSIGN COMPONENT LS_TEMPL_I-FIELD_NAME OF STRUCTURE <FS_LINE> TO <FS_VALUE>.
          IF SY-SUBRC = 0.
            <FS_VALUE> = LV_VALUE.
          ENDIF.

        ENDLOOP.

        AT END OF ITEM.
          APPEND <FS_LINE> TO <FT_TABLE>.
          CLEAR <FS_LINE>.
        ENDAT.
      ENDLOOP.
    ENDIF.
  ENDIF.

  "取审批者信息
  IF LS_APP_CNFG-WFCONFID IS NOT INITIAL AND IT_APPROVER[] IS INITIAL.
    CALL FUNCTION 'YBC_SWS_GET_APPROVER'
      EXPORTING
        IV_WFCONFID    = LS_APP_CNFG-WFCONFID
*       IV_APPLEVEL    =
        IV_FIRST_LEVEL = 'X'
      IMPORTING
        ES_MSG         = ES_MSG
      TABLES
        IT_REQ_HEADER  = LT_FVALUE_H1
        ET_APPROVER    = IT_APPROVER.

    IF ES_MSG-TYPE = 'E'.
      RETURN.
    ENDIF.
  ENDIF.

  "数据保存准备
  PERFORM FRM_GET_NUMBER CHANGING LS_REQUEST-REQUESTID.
  IF LS_REQUEST-REQUESTID IS INITIAL.
    ES_MSG-TYPE = 'E'.
    MESSAGE S007(YBC_SWS) INTO ES_MSG-MESSAGE.
    RETURN.
  ENDIF.

  LS_REQUEST-APPID = LS_APP-APPID.
  LS_REQUEST-BUSINESSID  = IV_BUSINESSID.
  TRY.
      LS_REQUEST-UUID  = CL_SYSTEM_UUID=>CREATE_UUID_C32_STATIC( ).
    CATCH CX_UUID_ERROR.
      ES_MSG-TYPE = 'E'.
      MESSAGE S009(YBC_SWS) INTO ES_MSG-MESSAGE.
      RETURN.
  ENDTRY.

  CLEAR LS_TEMPL.
  READ TABLE LT_TEMPL INTO LS_TEMPL WITH KEY LANGUAGE = '1'.
  IF SY-SUBRC <> 0.
    READ TABLE LT_TEMPL INTO LS_TEMPL WITH KEY LANGUAGE = SPACE.
  ENDIF.
  LS_REQUEST-CATEGORY1    = LS_TEMPL-CATEGORY.
  LS_REQUEST-SUBCATEGORY1 = LS_TEMPL-SUBCATEGORY.
  LS_REQUEST-SUBJECT1     = LS_TEMPL-SUBJECT.
  REPLACE ALL OCCURRENCES OF '&1' IN LS_REQUEST-SUBJECT1 WITH IV_BUSINESSID.

  CLEAR LS_TEMPL.
  READ TABLE LT_TEMPL INTO LS_TEMPL WITH KEY LANGUAGE = 'E'.
  LS_REQUEST-CATEGORY2    = LS_TEMPL-CATEGORY.
  LS_REQUEST-SUBCATEGORY2 = LS_TEMPL-SUBCATEGORY.
  LS_REQUEST-SUBJECT2     = LS_TEMPL-SUBJECT.
  REPLACE ALL OCCURRENCES OF '&1' IN LS_REQUEST-SUBJECT2 WITH IV_BUSINESSID.

  LOOP AT LT_TEMPL INTO LS_TEMPL WHERE LANGUAGE = 'Z'.
    LS_REQUEST-CATEGORY3    = LS_TEMPL-CATEGORY.
    LS_REQUEST-SUBCATEGORY3 = LS_TEMPL-SUBCATEGORY.
    LS_REQUEST-SUBJECT3     = LS_TEMPL-SUBJECT.
    REPLACE ALL OCCURRENCES OF '&1' IN LS_REQUEST-SUBJECT3 WITH IV_BUSINESSID.
    EXIT.
  ENDLOOP.

  LS_REQUEST-STATUS = GC_STATUS_SUBMITTED.
  MESSAGE S026(YBC_SWS) INTO LS_REQUEST-MESSAGE.

  LS_REQUEST-MOD         = GC_MOD_NEW.
  LS_REQUEST-CDATE       = SY-DATUM.
  LS_REQUEST-CTIME       = SY-UZEIT.
  LS_REQJSON-REQUESTID = LS_REQUEST-REQUESTID.
  LS_REQJSON-HEADJSON = /UI2/CL_JSON=>SERIALIZE( DATA = LT_FVALUE_H ).
  IF LT_ITEM_H[] IS NOT INITIAL.
    LS_REQJSON-ITEMHJSON = /UI2/CL_JSON=>SERIALIZE( DATA = LT_ITEM_H ).
    LS_REQJSON-ITEMRJSON = /UI2/CL_JSON=>SERIALIZE( DATA = <FT_TABLE> ).
  ENDIF.
  LS_REQJSON-ACTIONJSON = /UI2/CL_JSON=>SERIALIZE( DATA = LT_APP_ACTION ).

  "审批者处理
  LOOP AT IT_APPROVER.
    TRANSLATE IT_APPROVER-EXTUSID TO UPPER CASE.
    TRANSLATE IT_APPROVER-SAPID TO UPPER CASE.
    TRANSLATE IT_APPROVER-EMAIL TO UPPER CASE.
    " TRANSLATE IT_APPROVER-TEL TO UPPER CASE.
    MODIFY IT_APPROVER.
  ENDLOOP.

  SELECT *
    INTO TABLE LT_USER
    FROM YBC_SWS_USER
    FOR ALL ENTRIES IN IT_APPROVER
   WHERE EXTUSID = IT_APPROVER-EXTUSID
     OR SAPID = IT_APPROVER-SAPID.
  "OR EMAIL = IT_APPROVER-EMAIL
  "OR TEL = IT_APPROVER-TEL.

*  LOOP AT LT_USER INTO LS_USER.
*    CLEAR LV_PERSNUMBER.
*    SELECT SINGLE
*      PERSNUMBER
*      INTO LV_PERSNUMBER
*      FROM USR21
*      WHERE BNAME = LS_USER-SAPID.
*    IF SY-SUBRC = 0.
*      SELECT SINGLE
*        NAME_TEXT
*        INTO LS_USER-NAME1
*        FROM ADRP
*        WHERE PERSNUMBER = LV_PERSNUMBER.
*      CONCATENATE LS_USER-NAME1 '(' LS_USER-SAPID ')'
*        INTO LS_USER-NAME1.
*    ENDIF.
*    MODIFY LT_USER FROM LS_USER.
*  ENDLOOP.
*  MODIFY YBC_SWS_USER FROM TABLE LT_USER.


  "检查审批者是否存在，不存在则创建
  LOOP AT IT_APPROVER.
    CLEAR LS_USER.
    IF IT_APPROVER-EXTUSID IS NOT INITIAL.
      READ TABLE LT_USER INTO LS_USER
        WITH KEY EXTUSID = IT_APPROVER-EXTUSID.
    ENDIF.

    IF LS_USER IS INITIAL.
      IF IT_APPROVER-SAPID IS NOT INITIAL.
        READ TABLE LT_USER INTO LS_USER
          WITH KEY SAPID = IT_APPROVER-SAPID.
      ENDIF.
    ENDIF.

*    IF LS_USER IS INITIAL.
*      IF IT_APPROVER-EMAIL IS NOT INITIAL.
*        READ TABLE LT_USER INTO LS_USER
*            WITH KEY EMAIL = IT_APPROVER-EMAIL.
*      ENDIF.
*    ENDIF.
*
*    IF LS_USER IS INITIAL.
*      IF IT_APPROVER-TEL IS NOT INITIAL.
*        READ TABLE LT_USER INTO LS_USER
*            WITH KEY EMAIL = IT_APPROVER-TEL.
*      ENDIF.
*    ENDIF.

    IF LS_USER IS INITIAL.
      MOVE-CORRESPONDING IT_APPROVER TO LS_USER.
      PERFORM FRM_CREATE_USER CHANGING LS_USER ES_MSG.
      IF ES_MSG-TYPE = 'E'.
        EXIT.
      ENDIF.
      APPEND LS_USER TO LT_USER.
    ENDIF.

    CLEAR LS_APPROVER.
    LS_APPROVER-REQUESTID = LS_REQUEST-REQUESTID.
    LS_APPROVER-APPROVERID = LS_USER-USERID.
    LS_APPROVER-APPLEVEL = IT_APPROVER-APPLEVEL.
    LS_APPROVER-APPLEVEL_DESC1 = IT_APPROVER-APPLEVEL_DESC1.
    LS_APPROVER-APPLEVEL_DESC2 = IT_APPROVER-APPLEVEL_DESC2.
    LS_APPROVER-APPLEVEL_DESC3 = IT_APPROVER-APPLEVEL_DESC3.
    LS_APPROVER-COUNTERSIGN = IT_APPROVER-COUNTERSIGN.
    LS_APPROVER-RDATE = SY-DATUM.
    LS_APPROVER-RTIME = SY-UZEIT.
    APPEND LS_APPROVER TO LT_APPROVER.
  ENDLOOP.
  IF ES_MSG-TYPE = 'E'.
    RETURN.
  ENDIF.

  CLEAR LS_CREATOR.
  SELECT SINGLE
    *
    INTO LS_CREATOR
    FROM YBC_SWS_USER
   WHERE SAPID = IV_UNAME.
  IF SY-SUBRC <> 0.
    LS_CREATOR-SAPID = IV_UNAME.
    PERFORM FRM_CREATE_USER CHANGING LS_CREATOR ES_MSG.
    IF ES_MSG-TYPE = 'E'.
      RETURN.
    ENDIF.
  ENDIF.
  LS_REQUEST-USERID = LS_CREATOR-USERID.

  "附件处理
  LOOP AT IT_ATTACH.
    CLEAR LS_ATTACH_DB.
    MOVE-CORRESPONDING IT_ATTACH TO LS_ATTACH_DB.

    TRY .
        LS_ATTACH_DB-ATTACHID = CL_SYSTEM_UUID=>CREATE_UUID_C32_STATIC( ).
      CATCH CX_UUID_ERROR INTO DATA(REF_CX).
    ENDTRY.
    LS_ATTACH_DB-REQUESTID = LS_REQUEST-REQUESTID.
    LS_ATTACH_DB-ORIGIN = 'FROM REQUEST'. "来自于请求上传
    IF LS_ATTACH_DB-CONTENT IS NOT INITIAL.
      LS_ATTACH_DB-FILESIZE = XSTRLEN( LS_ATTACH_DB-CONTENT ).
      LS_ATTACH_DB-URL = '/sap/opu/odata/sap/YBC_SWIFTSAP_SRV/AttachSet(''' && LS_ATTACH_DB-ATTACHID && ''')/$value'.
    ENDIF.

    IF LS_ATTACH_DB-MIMETYPE IS INITIAL.
      CALL FUNCTION 'SKWF_MIMETYPE_OF_FILE_GET'
        EXPORTING
          FILENAME = LS_ATTACH_DB-FILENAME
        IMPORTING
          MIMETYPE = LS_ATTACH_DB-MIMETYPE.
    ENDIF.
    LS_ATTACH_DB-USERID = LS_CREATOR-USERID.
    LS_ATTACH_DB-USERNAME = LS_CREATOR-NAME1 && LS_CREATOR-NAME2 && LS_CREATOR-NAME3.
    LS_ATTACH_DB-UPLOADDATE  = SY-DATUM.
    LS_ATTACH_DB-UPLOADTIME  = SY-UZEIT.

    APPEND LS_ATTACH_DB TO LT_ATTACH_DB.
  ENDLOOP.

  LOOP AT LT_FVALUE_H1 INTO LS_FVALUE_H1.
    CLEAR LS_REQHEAD.
    MOVE-CORRESPONDING LS_FVALUE_H1 TO LS_REQHEAD.
    LS_REQHEAD-REQUESTID = LS_REQUEST-REQUESTID.

    APPEND LS_REQHEAD TO LT_REQHEAD.
  ENDLOOP.

  MODIFY YBC_SWS_REQUEST FROM LS_REQUEST.
  MODIFY YBC_SWS_REQHEAD FROM TABLE LT_REQHEAD.
  MODIFY YBC_SWS_REQJSON FROM LS_REQJSON.
  MODIFY YBC_SWS_APPROVER FROM TABLE LT_APPROVER.
  MODIFY YBC_SWS_ATTACH FROM TABLE LT_ATTACH_DB.

  COMMIT WORK AND WAIT.

  IF SY-SUBRC <> 0.
    ROLLBACK WORK.
    ES_MSG-TYPE = 'E'.
    MESSAGE S025(YBC_SWS) INTO ES_MSG-MESSAGE.
    RETURN.
  ELSE.

    ES_MSG-TYPE = 'S'.
    MESSAGE S008(YBC_SWS) INTO ES_MSG-MESSAGE.
  ENDIF.

  "发送邮件
  DATA:
    LV_MAIL_ERROR TYPE C,
    LS_MAIL       TYPE YBCS_SWS_MAIL,
    LS_MAIL_MSG   TYPE BAPIRET2.

  IF LS_APP_CNFG-FLG_SEND_MAIL IS NOT INITIAL.
    LOOP AT LT_USER INTO LS_USER WHERE EMAIL IS NOT INITIAL .
      CLEAR: LS_MAIL, LS_MAIL_MSG, LV_MAIL_ERROR.
      "LS_USER 即为审批者
      PERFORM FORM_PREPARE_MAIL_DATA
        USING LS_APP_CNFG-MAIL_LANGU LS_CREATOR LS_USER LS_REQUEST
        CHANGING LS_MAIL LV_MAIL_ERROR.
      IF LV_MAIL_ERROR IS NOT INITIAL.
        CONTINUE.
      ENDIF.

      CALL FUNCTION 'YBC_SWS_SEND_MAIL'
        EXPORTING
          IS_MAIL = LS_MAIL
        IMPORTING
          ES_MSG  = LS_MAIL_MSG.
    ENDLOOP.
  ENDIF.
ENDFUNCTION.
